home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c++ / 9 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  2.4 KB

  1. Path: chronicle.mti.sgi.com!news
  2. From: austern@mti.sgi.com (Matt Austern)
  3. Newsgroups: comp.std.c++
  4. Subject: Re: RTTI implementation...
  5. Date: 05 Jan 1996 21:01:45 GMT
  6. Organization: SGI
  7. Approved: austern@mti.sgi.com (Matt Austern)
  8. Message-ID: <AUSTERN.96Jan5115830@isolde.mti.sgi.com>
  9. References: <4ciauq$2e1@trojan.neta.com>
  10. Reply-To: austern@mti.sgi.com
  11. NNTP-Posting-Host: isolde.mti.sgi.com
  12. In-Reply-To: jonnyg@trojan.neta.com's message of 05 Jan 1996 19:32:45 GMT
  13.  
  14.  
  15.  
  16.  
  17. In article <4ciauq$2e1@trojan.neta.com> jonnyg@trojan.neta.com (J. Greenblatt) writes:
  18.  
  19. >     // This is a non RTTI enabled class, no RTTI overhead, no RTTI keywords
  20. >     class foo
  21. >     {
  22. >         int i;
  23. >     };
  24.  
  25. Actually, that's already more or less the case.  RTTI basically
  26. consists of two parts, dynamic_cast and typeid; in both cases, you
  27. need to distinguish between an expression's static type (its type as
  28. known at compile time) and its dynamic type (the type of an object
  29. pointed to by a pointer or reference).  The two can differ, of course,
  30. since a base class pointer can point to an object of derived class.
  31.  
  32. If you're simply working with an expression's static type, then there
  33. doesn't need to be any runtime overhead: everything can be done at
  34. compile time.  There's only overhead if you want to find the dynamic
  35. type of an object: that's the only case when you're really doing RTTI
  36. at all.  
  37.  
  38. The standard distinguishes between polymorphic classes (ones that
  39. declare or inherit virtual functions) and nonpolymorphic classes.  You
  40. can still use dynamic_cast and typeid for nonpolymorphic classes, but
  41. only to work with an expression's static type.  The section on
  42. dynamic_cast says "Otherwise [i.e. for downcasting and conversion from
  43. void*], v shall be a pointer to or an lvalue of a polymorphic type",
  44. and the section on typeid says "If the expression is an lvalue of
  45. polymorphic type (_class.virtual_), the type_info for the complete
  46. object (_class.base.init_) referred to is the result."  
  47.  
  48. There is already overhead associated with polymorphic classes (the
  49. virtual table); RTTI adds a little bit more, but it shouldn't add any
  50. overhead to nonpolymorphic classes, which are the ones where
  51. presumably one cares about such things.
  52.  
  53. Polymorphic classes are defined in section 10.3 of the working paper, 
  54. and dynamic_cast and typeid are described in sections 5.2.6 and 5.2.7
  55. respectively.
  56.  
  57.  
  58. Matt Austern
  59. SGI: MTI Compilers Group
  60. austern@isolde.mti.sgi.com
  61. -- 
  62. Matt Austern
  63. SGI: MTI Compilers Group
  64. austern@isolde.mti.sgi.com
  65.